<?php session_start();
require('../globals/setup.php');
$smarty = new Smarty_Kalender;	

$host  = $_SERVER['HTTP_HOST'];
if ($_SERVER['SERVER_NAME'] == 'localhost'){$uri= URI_L;}else{$uri= URI_P;}
if (!isset($_SESSION['id_user']) || $_SESSION['id_user']=='') {
	echo "<script language=Javascript> location.href='http://$host$uri/index.php'; </script>";	
}else{
	
	if (!is_object($bd)){	
		require("../globals/Db.class.php");
		require("../globals/Conf.class.php");
		$bd=Db::getInstance();
	}
	if (!is_object($util)){	
		include("../globals/Util.class.php");
		$util=Util::getInstance();
	}
	
	$util->debugOn();
	
	if (!($util->administrarTiempoSesion())){
		echo "<script language=Javascript> location.href='http://$host$uri/timeout.php'; </script>";
	}else if (!($util->esCliente())){
		echo "<script language=Javascript> location.href='http://$host$uri/sinAcceso.php'; </script>";
	}
	
	$id_user = $_SESSION['id_user'];	
	$idFac = $_GET['fid'];
	
	
	$query_arch = "select f.estado, f.fecha_creacion, f.numero, ot.id as id_ot, u.cuit, u.direccion, u.razon_social from facturas f, ordenes_trabajo ot, usuarios u where f.id_ot = ot.id and ot.id_cliente = $id_user and u.id = $id_user and f.id = $idFac";
	$smarty->assign("mensaje_vacio", "No se encontraron resultados.");
	
	//echo($query_arch);	
		
	$resultado = $bd->eje($query_arch);		
	$num_rows = $bd->numRows($resultado);
	
	if ($num_rows==1){	
		$row = $bd->fila($resultado);
			
		$smarty->assign("numero", $row["numero"]);
		$smarty->assign("cuit", $row["cuit"]);
		$smarty->assign("direccion", $row["direccion"]);
		$smarty->assign("razon_social", $row["razon_social"]);
		
		$smarty->assign("fecha_creacion", substr($row["fecha_creacion"],8,2)."/".substr($row["fecha_creacion"],5,2)."/".substr($row["fecha_creacion"],0,4));
		if ($row["estado"]=='E'){
			$smarty->assign("estado", "EMITIDA");
		}else if ($row["estado"]=='F'){
			$smarty->assign("estado", "FACTURADA");
		}else if ($row["estado"]=='A'){
			$smarty->assign("estado", "ANULADA");
		}
			
		$array_resultado = $util->getAllElementsWithValuesForThisOT($bd, $row["id_ot"]);	
		$smarty->assign("array_resultado", $array_resultado);
		$subtotal = $util->getTotalAmountFromThisOT($bd, $row["id_ot"]);
		$smarty->assign("subtotal", $subtotal);
		$iva = (intval($subtotal)*(.21));
		$total = $subtotal + $iva;
		$smarty->assign("iva", $iva);
		$smarty->assign("total", $total);
	}else{
		$smarty->assign("numero", "");
		$smarty->assign("cuit", "");
		$smarty->assign("direccion", "");
		$smarty->assign("razon_social", "");
		$smarty->assign("array_resultado", array());
		$smarty->assign("subtotal", "");
		$smarty->assign("iva", "");
		$smarty->assign("total", "");
		$smarty->assign("fecha_creacion","");
	}
	$menuItem = "misfacturas";
	$smarty->assign("menuItem", $menuItem);
	include("../globals/menuIzquierdaClientes.php");
	
	$smarty->display("clientes/detalleFactura.tpl");

} //del if grande ?>